package com.inspur.dgov.vlbod.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;

/**
 * 加解密类
 *
 * @Author chiyupei
 * @Date 2023/5/28 18:42
 * @Version 1.0
 * @Description 加解密类
 */
public class Secures {

    private static String privateKey = "738329f9f39bd7d6c44e64c00eaa73420c1892d0a2069030ac0d447d9538f6d3";

    private static String publicKey = "04d1fde754174be00a1086a6ee369cff0e408a7320c23a05deb2ef3b23e0c9d130eb0065844c9491beff8501ad5255ba863d484503d9a683359bfcaa356e2a47b4";


    /**
     * sm2解密
     *
     * @param encryptStr 加密字符串
     * @return 解密结果
     */
    public static String sm2Decrypt(String encryptStr) {
        return sm2Decrypt("04" + encryptStr, privateKey, publicKey);
    }

    /**
     * sm2解密
     *
     * @param encryptStr 加密字符串
     * @param privateKey 私钥
     * @param publicKey  公钥
     * @return 解密结果
     */
    public static String sm2Decrypt(String encryptStr, String privateKey, String publicKey) {
        if (StrUtil.isNotBlank(encryptStr)) {
            SM2 sm2 = SmUtil.sm2(privateKey, publicKey);
            encryptStr = StrUtil.utf8Str(sm2.decryptFromBcd(encryptStr, KeyType.PrivateKey));
        }
        return encryptStr;
    }


}
